//
//  XJTabBar.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/2.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJTabBar.h"
// Controller
#import "XJLoginViewController.h"
#import "XJNavViewController.h"

@interface XJTabBar ()

@property (strong, nonatomic) NSMutableArray *tabBarItems;

@end

@implementation XJTabBar

#pragma mark - Lifecycle

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if (self) {
        [self config];
    }
    
    return self;
}

#pragma mark - Private Method

- (void)config {
    self.backgroundColor = [UIColor whiteColor];
    UIImageView *topLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, -5, KWidth, 5)];
    topLine.image = [UIImage imageNamed:@"tapbar_top_line"];
    [self addSubview:topLine];
    
}

- (void)setSelectedIndex:(NSInteger)index {
    for (XJTabBarItem *item in self.tabBarItems) {
        if (item.tag == index) {
            item.selected = YES;
        } else {
            item.selected = NO;
        }
    }
    
    UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
    UITabBarController *tabBarController = (UITabBarController *)keyWindow.rootViewController;
    if (tabBarController) {
        if (index == 0 || index ==1) {
            
            tabBarController.selectedIndex = index;
        }else {
            // 需要判断是否登录
            if ([XJUserModel xj_isLogin]) {
                tabBarController.selectedIndex = index;
            }else {
                
                XJNavViewController *loginNAVC = [[XJNavViewController alloc] initWithRootViewController:[[XJLoginViewController alloc] init]];
                [tabBarController presentViewController:loginNAVC animated:YES completion:nil];
            }
        }
    }
}

#pragma mark - Touch Event

- (void)itemSelected:(XJTabBarItem *)sender {
    XJLog(@"sender.tag= %ld",(long)sender.tag);
    

    // 5-11 bo
    [self setSelectedIndex:sender.tag];

//    if (sender.tabBarItemType != XJTabBarItemTypeRise) {
//        [self setSelectedIndex:sender.tag];
//    } else {
//        if (self.delegate) {
//            if ([self.delegate respondsToSelector:@selector(tabBarDidSelectedRiseButton)]) {
//                [self.delegate tabBarDidSelectedRiseButton];
//            }
//        }
//    }
}

////获取到当前所在的视图
//- (UIViewController *)presentingVC{
//    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
//    if (window.windowLevel != UIWindowLevelNormal){
//        NSArray *windows = [[UIApplication sharedApplication] windows];
//        for(UIWindow * tmpWin in windows){
//            if (tmpWin.windowLevel == UIWindowLevelNormal){
//                window = tmpWin;
//                break;
//            }
//        }
//    }
//    UIViewController *result = window.rootViewController;
//    while (result.presentedViewController) {
//        result = result.presentedViewController;
//    }
//    if ([result isKindOfClass:[MainTabBarViewController class]]) {
//        result = [(MainTabBarViewController *)result selectedViewController];
//    }
//    if ([result isKindOfClass:[UINavigationController class]]) {
//        result = [(UINavigationController *)result topViewController];
//    }
//    return result;
//}

#pragma mark - Setter

- (void)setTabBarItemAttributes:(NSArray<NSDictionary *> *)tabBarItemAttributes {
    _tabBarItemAttributes = tabBarItemAttributes.copy;
    
    CGFloat itemWidth = KWidth / _tabBarItemAttributes.count;
    CGFloat tabBarHeight = CGRectGetHeight(self.frame);
    NSInteger itemTag = 0;
    BOOL passedRiseItem = NO;
    
    _tabBarItems = [NSMutableArray arrayWithCapacity:_tabBarItemAttributes.count];
    for (id item in _tabBarItemAttributes) {
        if ([item isKindOfClass:[NSDictionary class]]) {
            NSDictionary *itemDict = (NSDictionary *)item;
            
            XJTabBarItemType type = [itemDict[kXJTabBarItemAttributeType] integerValue];
            CGRect frame = CGRectMake(itemTag * itemWidth + (passedRiseItem ? itemWidth : 0), 0, itemWidth, tabBarHeight);
            
            XJTabBarItem *tabBarItem = [self tabBarItemWithFrame:frame title:itemDict[kXJTabBarItemAttributeTitle] normalImageName:itemDict[kXJTabBarItemAttributeNormalImageName] selectedImageName:itemDict[kXJTabBarItemAttributeSelectedImageName] tabBarItemType:type];
            if (itemTag == 0) {
                tabBarItem.selected = YES;
            }
            
            [tabBarItem addTarget:self action:@selector(itemSelected:) forControlEvents:UIControlEventTouchUpInside];
            
            //5-11 bo
            tabBarItem.tag = itemTag;
            itemTag++;

//            if (tabBarItem.tabBarItemType != XJTabBarItemTypeRise) {
//                tabBarItem.tag = itemTag;
//                itemTag++;
//            } else {
//                passedRiseItem = YES;
//            }
            
            [_tabBarItems addObject:tabBarItem];
            [self addSubview:tabBarItem];
        }
    }

}

- (XJTabBarItem *)tabBarItemWithFrame:(CGRect)frame
                                title:(NSString *)title
                      normalImageName:(NSString *)normalImageName
                    selectedImageName:(NSString *)selectedImageName
                       tabBarItemType:(XJTabBarItemType)tabBarItemType {
    XJTabBarItem *item = [[XJTabBarItem alloc] initWithFrame:frame];
    [item setTitle:title forState:UIControlStateNormal];
    [item setTitle:title forState:UIControlStateSelected];
    item.titleLabel.font = TABBAR_ITEM_FONT;
    UIImage *normalImage = [UIImage imageNamed:normalImageName];
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    [item setImage:normalImage forState:UIControlStateNormal];
    [item setImage:selectedImage forState:UIControlStateSelected];
    [item setTitleColor:ITEM_UNSELECTED_COLOR forState:UIControlStateNormal];
    [item setTitleColor:ITEM_SELECTED_COLOR forState:UIControlStateSelected];
    item.tabBarItemType = tabBarItemType;
    
    return item;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
